//
//  SHSaveTool.m
//  UI_CarHome
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHSaveTool.h"

@implementation SHSaveTool

+ (void)isHaveFolder
{
    // 1.找沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 2.拼接文件夹名
    NSString *carHomeFile = [sandBoxPath stringByAppendingPathComponent:@"CarHome"];
    // 创建一个文件夹的管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //
    NSLog(@"%d", [manager fileExistsAtPath:carHomeFile]);
    
    if (![manager fileExistsAtPath:carHomeFile]) {
        // 创建文件夹
        [manager createDirectoryAtPath:carHomeFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", carHomeFile);
}

+ (BOOL)isHaveCarHomeInPlist:(SHModel *)model
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CarHome/carHome.plist"];
    // 反归档
    NSArray *carHomeArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    // 遍历
    for (SHModel *temp in carHomeArr) {
       
            if ([temp.myId isEqualToNumber:model.myId] ) {
                return NO;
            }

        
   
    }
    return YES;
}

+ (void)saveCarHomeToPlist:(SHModel *)model
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CarHome/carHome.plist"];
    // 直接归档会覆盖掉之前的数据,所以先进行反归档,然后添加进去之后在进行归档操作
    NSMutableArray *carHomeArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (carHomeArr == nil) {
        // 如果为空,先进行初始化
        carHomeArr = [NSMutableArray array];
    }
    [carHomeArr addObject:model];
    // 归档
    [NSKeyedArchiver archiveRootObject:carHomeArr toFile:docPath];
    NSLog(@"%@", docPath);
}

+ (void)cancelCarHomeInPlist:(SHModel *)model
{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"CarHome/carHome.plist"];
    
    // 反归档
    NSMutableArray *carHomeArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    for (SHModel *temp in carHomeArr) {
       
            if ([temp.myId isEqualToNumber:model.myId]) {
                // 移除
                [carHomeArr removeObject:temp];
                break;
            }

    }
    // 归档
    [NSKeyedArchiver archiveRootObject:carHomeArr toFile:docPath];
}


+ (void)clearCache:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *chileFiles = [fileManager subpathsAtPath:path];
        for (NSString *fileName in chileFiles) {
            //
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }

}
@end
